package com.yx.controller.demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.dao.PersonMapper;
import com.yx.entity.Person;
import com.yx.util.DateUtils;
import com.yx.util.MybatisUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Classname PersonController
 * @Description TODO:
 * @Date 2021/05/14 19:34
 * @Created by yx
 */
//@WebServlet("/person")
public class PersonController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        switch (method) {
            case "login":
                login(request, response);
                return;
            case "register":
                register(request, response);
                return;
            case "getAll":
                getAll(request, response);
                return;
            default:
                response.sendRedirect("/login.html");
                return;
        }
    }

    private void getAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer pageNum = null;
        String num = request.getParameter("pageNum");
        if (num==null){
            pageNum = 1;
        }else {
            pageNum=Integer.valueOf(request.getParameter("pageNum"));
        }
        PersonMapper mapper = MybatisUtils.getMapper(PersonMapper.class);
        PageHelper.startPage(pageNum,5);
        List<Person> personList = mapper.selectAll();
        PageInfo<Person> pageInfo = new PageInfo<Person>(personList);
        MybatisUtils.closeSession();
        System.out.println(pageInfo);
        ObjectMapper objectMapper = new ObjectMapper();
        String string = objectMapper.writeValueAsString(pageInfo);
        response.getWriter().print(string);
    }

    private void register(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String name = request.getParameter("username");
        Integer age = Integer.valueOf(request.getParameter("age"));
        String password = request.getParameter("password");
        java.sql.Date bornDate = DateUtils.dataToSql(DateUtils.stringToDate(request.getParameter("bornDate")));
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        Person person = new Person(name, age, password, bornDate, email, address);
        System.out.println(person);
        PersonMapper mapper = MybatisUtils.getMapper(PersonMapper.class);
        Integer integer = mapper.insertPerson(person);
        MybatisUtils.commit();
        if (integer > 0) {
            response.getWriter().print("success");
        } else {
            response.getWriter().print("fail");
        }

    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws
            IOException, ServletException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username + "=================");
        PersonMapper mapper = MybatisUtils.getMapper(PersonMapper.class);
        Person login = mapper.login(username, password);
        MybatisUtils.closeSession();
        if (login != null) {
            response.getWriter().print("success");
        } else {
            response.getWriter().print("fail");
        }
    }
}
